// becomeBeautiful/pages/bmmj_detail/index.js
import {
    getCateInfo
} from './util.js'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgUrl: "",
        statusBarHeight: "",
        screenHeight: "",
        markerList: [],
        pie: "",
        imgHeight: "",
        dotInfo:[],
        isFX:true


    },

    

    /**
     * 生命周期函数--监听页面显示
     */
    onLoad(option) {
        console.log(option)
        wx.getSystemInfo({
            success: res => {
                console.log("res", res)
                this.setData({
                    screenHeight: res.screenHeight,
                    statusBarHeight: res.statusBarHeight,
                    imgUrl: getApp().globalData.tempPhoto,
                    screenWidth: res.screenWidth,
                    imgHeight:option.height/getApp().globalData.pie,
                    pie: getApp().globalData.pie
                })
            },
        })
        
        console.log("描点缩放比", getApp().globalData.pie)
    },
    // 获取img 的高度
    getZNimgHeight() {
        var query = wx.createSelectorQuery();
        //选择id
        query.select('#zn_img').boundingClientRect()
        query.exec((res) => {
            console.log("zn_img高度", res);
            // let distanceTop = res[0].top * 2
            this.setData({
                imgHeight: this.data.screenHeight -  res[0].top * 2,
                // distanceTop: distanceTop
            })
        })
    },
    callDone() {
        this.setData({
            openSM: false
        })
        wx.showLoading({
            title: '解析中...',
        })
        setTimeout(() => {
            const ctx = wx.createCanvasContext('canvas')
            // console.log( this.data.markerList)
            // 绘制  描点
            this.data.markerList.forEach(item => {
                ctx.rect(item.x / this.data.pie, item.y / this.data.pie, 1, 1)
                ctx.setFillStyle('rgb(27,134,241)')
                ctx.fill()
            })
            ctx.draw(true, () => {
                // 绘制四个部位点
                let dotInfo = [
                    ['eyedot', {
                        x: this.data.dotInfo.eyedotX,
                        y: this.data.dotInfo.eyedotY
                    }],
                    ['facedot', {
                        x: this.data.dotInfo.facedotX,
                        y: this.data.dotInfo.facedotY
                    }],
                    ['lipdot', {
                        x: this.data.dotInfo.lipdotX,
                        y: this.data.dotInfo.lipdotY
                    }],
                    ['nosedot', {
                        x: this.data.dotInfo.nosedotX,
                        y: this.data.dotInfo.nosedotY
                    }]
                ]
                dotInfo.forEach(item => {
                    ctx.rect(item[1].x / this.data.pie, item[1].y / this.data.pie, 20, 20,2 * Math.PI)
                    ctx.setFillStyle('white')
                    ctx.fill()
                })
                ctx.draw(true, () => {
                    this.setData({
                        f1: false,
                        f2: false,
                        f3: false,
                        f4: false,
                        f5: false
                    })
                    wx.hideLoading()
                    console.log("后续操作")
                })
            })
        }, 2000)
    },


    // 获取AI 描点 集合
    getMarkList() {
        wx.getFileSystemManager().readFile({
            filePath: this.data.imgUrl, //选择图片返回的相对路径
            encoding: 'base64', //编码格式
            success: res => { //成功的回调
                let data = {
                    openid: wx.getStorageSync('ACCESS_TOKEN'),
                    nickname: wx.getStorageSync('appUserInfo').nickName,
                    avatar: wx.getStorageSync('appUserInfo').avatarUrl,
                    unionid: wx.getStorageSync('appUserInfo').union_id,
                    base64String: res.data,
                    hosId: '1'
                }

                wx.request({
                    method: "POST",
                    url: getApp().api.bmmj_znmxpd,
                    data: data,
                    header: {
                        'content-type': 'application/json'
                    },
                    success: (result => {
                        console.log("result",result)
                        if (!result.data.hasOwnProperty('message')) {
                            // 4个部位信息
                            let dotInfoXY = {
                                eyedotX: result.data.dotInfo.eyedotX,
                                eyedotY: result.data.dotInfo.eyedotY,
                                facedotX: result.data.dotInfo.facedotX,
                                facedotY: result.data.dotInfo.facedotY,
                                lipdotX: result.data.dotInfo.lipdotX,
                                lipdotY: result.data.dotInfo.lipdotY,
                                nosedotX: result.data.dotInfo.nosedotX,
                                nosedotY: result.data.dotInfo.nosedotY,
                            }

                            let dotInfo=[
                                ['eyedot', {
                                    x: dotInfoXY.eyedotX,
                                    y: dotInfoXY.eyedotY,
                                    text:result.data.eyeAdvice.text,
                                    advice:result.data.eyeAdvice.advice,
                                    standard:result.data.eyeAdvice.standard
                                }],
                                ['facedot', {
                                    x: dotInfoXY.facedotX,
                                    y: dotInfoXY.facedotY,
                                    text:result.data.faceAdvice.text,
                                    advice:result.data.faceAdvice.advice,
                                    standard:result.data.faceAdvice.standard
                                }],
                                ['lipdot', {
                                    x: dotInfoXY.lipdotX,
                                    y: dotInfoXY.lipdotY,
                                    text:result.data.lipAdvice.text,
                                    advice:result.data.lipAdvice.advice,
                                    standard:result.data.lipAdvice.standard
                                }],
                                ['nosedot', {
                                    x: dotInfoXY.nosedotX,
                                    y: dotInfoXY.nosedotY,
                                    text:result.data.noseAdvice.text,
                                    advice:result.data.noseAdvice.advice,
                                    standard:result.data.noseAdvice.standard
                                }]
                            ]

                            this.setData({
                                dotInfo: dotInfo
                            })
                            getApp().globalData.dotInfo=dotInfo
                            //landMark 标记点
                            let list = result.data.face.landmark
                            getCateInfo(list, res => {
                                console.log("markerList", res)
                                this.setData({
                                    // imgUrl: result.data.imgurl,
                                    markerList: this.reSetData(res, 50),
                                    // markerList: res
                                    openSM:false,
                                    f1:false,
                                    f2:false,
                                    f3:false,
                                    f4:false,
                                    f5:false,
                                    isFX:false

                                })
                                // this.callDone()

                                
                            })
                            //脸部范围
                            let face_rectangle = result.data.face.face_rectangle
                            this.setData({
                                face_rectangle: face_rectangle
                            })
                            // 前后图片对比
                            getApp().globalData.imgArr=[result.data.imgurl,result.data.after_img_url]

                            // 美学三维图参数
                            getApp().globalData.mxswArg={
                                age_score:result.data.age_score,
                                distance_score:result.data.distance_score,
                                smart_score:result.data.smart_score,
                            }

                        } else {
                            // let msg = result.data.msg
                            wx.showToast({
                                title: result.data.message,
                                icon: 'none',
                                duration: 2000
                            })

                            setTimeout(()=>{
                                wx.navigateBack()
                            },2500)
                        }
                    }),
                    fail: (fail => {
                        // console.log("err", fail)
                    })
                })
            }
        })
    },
    // 将数据拆分为多组
    reSetData(dataList, num) {
        let arr = [];
        let len = dataList.length;
        for (let i = 0; i < len; i += num) {
            arr.push(dataList.slice(i, i + num));
        }
        console.log('arr', arr)
        return arr;
    },
    back() {
        wx.navigateBack({
            delta: 1
        })
    },
    faceAnalysis() {
        this.setData({
            openSM: true
        })
        console.log("面容分析")
        this.getMarkList()
        this.startTimer()
    },
    // AI美学设计
    aestheticDesign(){
        wx.showLoading({
          title: '处理中...',
        })
        setTimeout(()=>{
            wx.hideLoading()
            wx.navigateTo({
                url: '../aestheticDesign/index',
              })
        },1500)
       
    },
    startTimer() {
        setTimeout(() => {
            this.setData({
                f1: true
            })
        }, 600)
        setTimeout(() => {
            this.setData({
                f2: true
            })
        }, 1200)
        setTimeout(() => {
            this.setData({
                f3: true
            })
        }, 1800)
        setTimeout(() => {
            this.setData({
                f4: true
            })
        }, 2400)
        setTimeout(() => {
            this.setData({
                f5: true,

            })
        }, 3000)
    },


})